package com.dal.activiti.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.activiti.bpmn.model.BaseElement;
import org.activiti.bpmn.model.ExtensionAttribute;
import org.activiti.bpmn.model.ExtensionElement;

import java.util.*;

/**
 * @description
 * @ClassName BpmnUtil
 * @Author jian
 * @Date 5:18 PM 2022/12/23
 **/
public class BpmnUtil {
    /**
     * 扩展->添加属性:
     * @param map
     * @return
     */
    public static void addExtensionProperty(BaseElement baseElement, Map<String, Object> map) {
        addExtensionProperty(baseElement, map, null);
    }


    /**
     * 扩展->添加属性:
     * @param map
     * @return
     */
    public static void addExtensionProperty(BaseElement baseElement, Map<String, Object> map, SimplePropertyPreFilter filter) {
        List<ExtensionElement> properties = getOrCreateExtensionProperties(baseElement);
        for (String name : map.keySet()) {
            Object value = map.get(name);

            ExtensionElement childElement = new ExtensionElement();
            childElement.setName("activiti:property");
            List<ExtensionAttribute> attributeList = new ArrayList<>();
            ExtensionAttribute attributeName = new ExtensionAttribute();
            attributeName.setName("name");
            attributeName.setValue(name);
            ExtensionAttribute attributeValue = new ExtensionAttribute();
            attributeValue.setName("value");
            attributeValue.setValue(JSON.toJSONString(value, filter));
            attributeList.add(attributeName);
            attributeList.add(attributeValue);

            Map<String, List<ExtensionAttribute>> attributes = new HashMap<>();
            attributes.put("attributes", attributeList);
            childElement.setAttributes(attributes);
            properties.add(childElement);
        }
    }


    /**
     * 获取properties扩展，没有则创建
     * @return
     */
    public static List<ExtensionElement> getOrCreateExtensionProperties(BaseElement baseElement) {
        List<ExtensionElement> extensionElements = getOrCreateExtensionElements(baseElement);
        Optional<ExtensionElement> first = extensionElements.stream().filter(v -> v.getName().equals("activiti:properties")).findFirst();
        if (!first.isPresent()) {
            ExtensionElement element = new ExtensionElement();
            element.setName("activiti:properties");
            extensionElements.add(element);
            Map<String, List<ExtensionElement>> childElements = new HashMap<>();
            List<ExtensionElement> propertyElementList = new ArrayList<>();
            childElements.put("property",propertyElementList);
            element.setChildElements(childElements);
            return propertyElementList;
        }
        return first.get().getChildElements().get("property");
    }



    /**
     * 获取extensionElements扩展，没有则创建
     * @return
     */
    public static List<ExtensionElement> getOrCreateExtensionElements(BaseElement baseElement) {
        if (baseElement.getExtensionElements()==null) {
            baseElement.setExtensionElements(new HashMap<>());
        }
        Map<String, List<ExtensionElement>> extensionElementsMap = baseElement.getExtensionElements();
        if (!extensionElementsMap.containsKey("extensionElements")) {
            extensionElementsMap.put("extensionElements", new ArrayList<>());
        }
        return extensionElementsMap.get("extensionElements");
    }
}
